import {ConfigDataEntity} from "../../../../../generate/entitas";
import BaseGenerator from "../../BaseGenerator";
import {TsGenerator} from "../../../../code_generater/ts/TsGenerator";
import GeneratorDecorator from "../../../GeneratorDecorator";
import MethodInfo from "../../../../MethodInfo";
import {CellData} from "../../../../CellData";
__dirname = __dirname.replace("/dist", "/src")


@GeneratorDecorator.generator("complexmultipleindex", "ts")
export default class ComplexMultipleIndex extends BaseGenerator{
	propertyList : Array<string>
	columnEntityList : Array<ConfigDataEntity>
	beforeInit() {
		this.dirPath = __dirname
	}

	onInit() {
		this.rootEntity.onGenerateHeadDataAction.value.push(this.onWriteHeadData.bind(this))
		this.rootEntity.onGenerateCodeAction.value.push(this.onWriteCode.bind(this))
		this.propertyList = this.checkData.propertyList
		if (this.propertyList == undefined){
			utils.LogErrorWithColumnEntity("[ComplexIndex] complexindex 需要配置 propertyList 数据", this.columnEntity)
		}
		this.initColumnEntityList()
	}

	initColumnEntityList(){
		this.columnEntityList = new Array<ConfigDataEntity>()
		let allColumnList = this.rootEntity.columnEntityList.value
		for (let i = 0; i < this.propertyList.length; i++) {
			let p = this.propertyList[i]
			for (let j = 0; j < allColumnList.length; j++) {
				let e = allColumnList[j]
				if (e.memberHeadData.value.name == p){
					this.columnEntityList.push(e)
					break
				}
			}
		}
	}


	getFuncName(){
		return `GetBy${this.propertyList.join("")}`
	}

	onWriteHeadData(generator : TsGenerator , entity : ConfigDataEntity) {
		let indexDict = new Map()
		let rowValList = this.rootEntity.rowCellList.value
		let propertyList = this.propertyList
		for (let c = 0; c < propertyList.length; c++) {
			for (let i = 0; i < rowValList.length; i++) {
				let row :CellData = rowValList[i]
				let value = row[c].value
				if (!indexDict.has(value)){
					indexDict.set(value, [])
				}
				indexDict.get(value).push(value)
			}
		}

		entity.headInfo.value.set(this.columnEntity.memberHeadData.value.name + "ComplexIndex", indexDict)
	}

	getInitMethodName(){
		return `Init${this.columnEntity.memberHeadData.value.name}ComplexIndex`
	}

	onWriteCode(generator : TsGenerator, entity : ConfigDataEntity){
		let dataTypeList = []
		let dataNameList = []
		for (let i = 0; i < this.columnEntityList.length; i++) {
			let e = this.columnEntityList[i]
			let entity_head = e.memberHeadData.value
			dataTypeList.push(entity_head.normalType)
			dataNameList.push(entity_head.name)
		}

		let ret = this.generateWithFile("template",
			{
				initMethodName:this.getInitMethodName(),
				methodName:this.getFuncName(),
				dataTypeList : dataTypeList,
				dataNameList : dataNameList
			})
		let methodInfo = new MethodInfo()
		methodInfo.initMethodName = this.getInitMethodName()
		methodInfo.methodContent = ret
		let methodMap = entity.indexInitMethodNameDict.value
		methodMap.set(this.getInitMethodName(), methodInfo)
	}





}